package com.cloudnote.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudnote.web.model.User;
import com.cloudnote.web.service.UserServiceI;
import com.cloudnote.web.util.FastJsonTool;
import com.cloudnote.web.util.LogUtils;


@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserServiceI UserManagerImpl;
	
	

//	 @RequestMapping(value = "/addAppeal", method = RequestMethod.POST)
//	    @ResponseBody
//	    public String addAppeal(@RequestParam(value = "name") String name,
//	                            @RequestParam(value = "town") String town,
//	                            @RequestParam(value = "village") String village,
//	                            @RequestParam(value = "telephone") String telephone,
//	                            @RequestParam(value = "appealtype") String appealtype,
	@RequestMapping("/addUser")
	@ResponseBody
	public String addUser(@RequestParam(value = "username") String username,
						  @RequestParam(value = "passwd") String passwd,
						  @RequestParam(value = "age") String age,
						  @RequestParam(value = "sex") String sex,
						  @RequestParam(value = "email") String email){
		System.out.println("@RequestMapping(addUser");
		User user = new User();
		user.setName(username);
		user.setPasswd(passwd);
		user.setAge(age);
		user.setSex(sex);
		user.setEmail(email);
		String id = UserManagerImpl.addUser(user);
		System.out.println("UserManagerImpl.addUser(user).id="+ id );
		String str = FastJsonTool.createJsonString(user);
		System.out.println(str);
		return id;
	}
	
	
	@RequestMapping("/getUserJson")
	@ResponseBody
	public String getUserJson(@RequestParam(value = "id") String id){
		System.out.println("getUser--->");
		User user = UserManagerImpl.getUserByID(id);
		String str = FastJsonTool.createJsonString(user);
		System.out.println("createJsonString=="+str);
		return str;
	}
	@RequestMapping("/getUser")
	@ResponseBody
	public User getUser(@RequestParam(value = "username") String username){
		System.out.println("getUser---username>"+username);
		User user = UserManagerImpl.getUserByName(username);
//		String str = FastJsonTool.createJsonString(user);
//		request.setAttribute("user", user);
//		System.out.println("createJsonString=="+str);
		return user;
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(User user,HttpServletRequest request){
		
		if(UserManagerImpl.updateUser(user)){
			user = UserManagerImpl.getUserByID(user.getId());
			request.setAttribute("user", user);
			return "forward:/user/getAllUser";
		}else{
			return "/error";
		}
		
	}
	
	@RequestMapping("/getAllUser")
	public String getAllUser(HttpServletRequest request){
		System.out.println("getAllUser");
		LogUtils.logInfo("getAllUser");
		List<User> user = UserManagerImpl.getAllUser();	
		request.setAttribute("user", user);
		
		return "/userManager";
	}
	
	@RequestMapping("/delUser")
	public void delUser(String id,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		if(UserManagerImpl.delUser(id)){
			System.out.println("user/delUser?id=");
			System.out.println("UserManagerImpl.delUser(id)");
			result = "{\"result\":\"success\"}";
		}
		PrintWriter out = null;
		response.setContentType("application/json");
		
		try {
			out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	


	
}
